/**
 * @author Mariano Albera
 */
var win = Ti.UI.currentWindow;
var xml = '';

/*
var ta1 = Titanium.UI.createTextArea({
	value:'Results here',
	height:100,
	width:300,
	top:10,
	font:{fontSize:10,fontFamily:'Helvetica Neue', fontWeight:'normal'},
	color:'#888',
	textAlign:'left',
	borderWidth:1,
	borderColor:'#bbb',
	borderRadius:1,
	scrollable:true
});
//win.add(ta1);
*/

var resultsLabel = Titanium.UI.createLabel({
    text:'',
    height:30,
    width:'auto',
    shadowColor:'black',
    shadowOffset:{x:2,y:2},
    color:'white',
    font:{fontSize:18,fontWeight:'bold'},
    top:5,
    textAlign:'center'
});
//win.add(resultsLabel)

var tableview = Titanium.UI.createTableView({
	style:Titanium.UI.iPhone.TableViewStyle.PLAIN,
	backgroundColor:'white',
	//separatorColor:'#390A0E',
		borderRadius:5,
		borderWidth:1,
	top:0,
	height:'auto',
	width:'auto'
});




win.addEventListener('focus', function() 
{ 
	var rd = []; tableview.data = rd;
	//ta1.value = win.resultsText;
	if(win.resultsText.length>0)
	{
		xml = Ti.XML.parseString(win.resultsText);
		parse(xml);
		win.add(tableview);
	}

 });
 
function parse(xml)
{
	var data =[];
	var section = Ti.UI.createTableViewSection();
    data.push(section);
	
	var flights = xml.documentElement.getElementsByTagName("flight");
	
	section.setHeaderTitle(flights.length + " results recieved in "+win.timeElapsed+"ms");
    //resultsLabel.text = flights.length + " results recieved in "+win.timeElapsed+"ms";
    
	Ti.API.info(flights.length);
	
	for (var i = 0; i < flights.length; i++) 
	{
		 var node = flights.item(i);
		
		 var arr = parseFlight(node);
		  
		 var row = buildRow3(arr);
          
		  section.add(row);
		  //data.push(row);
	}
	
	tableview.data = data;
}

function parseFlight(node)
{
	var price = node.getElementsByTagName('price').item(0).getElementsByTagName('priceFromWithTax').item(0).text;
	var origin = node.getElementsByTagName('departure').item(0).getElementsByTagName('city').item(0).text;
	var originIata = node.getElementsByTagName('departure').item(0).getElementsByTagName('code').item(0).text;
	var destination = node.getElementsByTagName('arrival').item(0).getElementsByTagName('city').item(0).text;
	var destinationIata = node.getElementsByTagName('arrival').item(0).getElementsByTagName('code').item(0).text;
	var carrier = node.getElementsByTagName('airline').item(0).getElementsByTagName('name').item(0).text;
	var carrierCode = node.getElementsByTagName('airline').item(0).getElementsByTagName('code').item(0).text;
	var depDate = node.getElementsByTagName('departure').item(0).getElementsByTagName('date').item(0).text;
	var arrDate = node.getElementsByTagName('arrival').item(0).getElementsByTagName('date').item(0).text;
	
	return {price:price,origin:origin,destination:destination,carrier:carrier,
			depDate:depDate,arrDate:arrDate,carrierCode:carrierCode,originIata:originIata,
			destinationIata:destinationIata};

}

/*
function buildRow(arr)
{
	var row = Ti.UI.createTableViewRow({height:60,backgroundColor:'white',opacity:0.8});
	  
	var labP = Ti.UI.createLabel({color:'blue',text:''+arr.price,borderWidth:0,
	   font:{fontSize:20, fontWeight:'bold'},top:0,left:5,height:'auto',width:100});
	  
	var labC = Ti.UI.createLabel({color:'black',text:''+arr.carrierCode,
	   font:{fontSize:20, fontWeight:'bold'},top:20,left:30,height:'auto',width:100});
	  
	var labR = Ti.UI.createLabel({color:'black',text: arr.origin + " - " + arr.destination,
	   font:{fontSize:20, fontWeight:'normal'},top:10,left:105,height:'auto',width:300});
	  
	var labD = Ti.UI.createLabel({color:'black',text: arr.depDate + ' to ' + arr.arrDate ,
	   font:{fontSize:14, fontWeight:'normal'},top:30,left:105,height:'auto',width:300});
	   
	row.add(labP);
    row.add(labR);
    row.add(labC);
    row.add(labD);
		  
	return row;
}

function buildRow2(arr)
{
	//var row = Ti.UI.createTableViewRow({height:60,backgroundColor:'white'});
	var row = Ti.UI.createTableViewRow();
		
		row.selectedBackgroundColor = '#fff';
		row.setBackgroundColor('#fff');
		row.height = 100;
		row.className = 'datarow';
		row.clickName = 'row';
		
	row.add(Ti.UI.createLabel({ color:'#999', font:{fontSize:18,fontWeight:'bold', fontFamily:'Arial'},
			left:5,top:5,height:40,width:100, text:arr.price+' €'}));
	
	row.add(Ti.UI.createImageView({
			defaultImage:'images/38-airplane.png',
			image: 'http://media.expedia.com/media/content/expus/graphics/static_content/fusion/v0.1b/images/airlines/sm'+arr.carrierCode.toUpperCase()+'.gif',
			//image: 'http://www.airlinelogos.aero/500px/'+arr.carrierCode.toUpperCase()+'.png',
			top:45,left:5,width:30,height:30,clickName:'image'}));
	
	row.add(Ti.UI.createLabel({color:'#576996',font:{fontSize:18,fontWeight:'bold', fontFamily:'Arial'},
			left:100,top:5,height:30,width:200,text:arr.origin + " - " + arr.destination	}));
	
	//nrow.filter = user.text;
	
	row.add(Ti.UI.createLabel({color:'#222',font:{fontSize:14,fontWeight:'normal', fontFamily:'Arial'},
			left:70,top:20,height:50,width:'auto',
			text:'Dep: ' + arr.depDate+ ' - Ret: ' + arr.arrDate}));

	row.add(Ti.UI.createView({backgroundImage:'images/commentButton.png',
			top:35,	right:50,width:20,height:20 }));
			
	return row;
}

*/

function buildRow3(arr)
{
	
	var row = Titanium.UI.createTableViewRow({title:"",height:80,left:0,width:"auto",borderRadius:20, hasDetail:true});
	
	var font_big = {fontFamily:"Arial",fontWeight:"Bold",fontSize:18};
	var font_std = {fontFamily:"Arial",fontWeight:"Bold",fontSize:14};
	var font_sm = {fontFamily:"Arial",fontWeight:"Normal",fontSize:12};

	row.add(Ti.UI.createLabel({text:"Duration: 2:30hs",color:"#0080FF",
		font:{fontFamily:"Arial",fontSize:10},height:18,left:245,top:-3,width:87}));	
	
	row.add(Ti.UI.createImageView({
			defaultImage:'../images/38-airplane.png',
			image: 'http://www.travelgenio.com/Content/images/airlines/sm'+arr.carrierCode.toUpperCase()+'.gif',
			height:50,left:5,top:20,width:'auto'}));
		
	row.add(Ti.UI.createLabel({text: arr.origin+" ("+arr.originIata+") to "+arr.destination+" ("+arr.destinationIata+")",
			font:font_std,height:'auto',left:80,top:22,width:'auto'}));
	
	row.add(Ti.UI.createLabel({text:arr.price + " eur",font:font_big,color:'#003366',
			height:'auto',left:5,top:0,width:100}));

	row.add(Ti.UI.createLabel({text:"Departing: "+arr.depDate+" 08:00",
		height:28,left:80,top:40,width:200, font:font_sm}));
	
	row.add(Ti.UI.createLabel({text:"Returning: "+arr.arrDate+" 17:30",
		height:28,left:80,top:55,width:200,font:font_sm}));
		
	//row.add(Ti.UI.createImageView({image:"../images/right.png",
		//height:'auto',left:280,top:42,width:'auto'}));

	return row;
}

